package wang.aweb.childcar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wang.aweb.childcar.model.dao.DonateLogMapper;
import wang.aweb.childcar.model.pojo.DonateLog;
import wang.aweb.childcar.model.pojo.Member;
import wang.aweb.childcar.model.vo.DonateLogVO;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.service.DonateService;
import wang.aweb.childcar.service.MemberService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DonateServiceImpl implements DonateService {
    @Autowired
    private DonateLogMapper donateLogMapper;
    @Autowired
    private MemberService memberService;
    @Override
    public PageInfo getList(Integer page, Integer limit){
        PageHelper.startPage(page,limit);
        List<DonateLog> donateLogList=donateLogMapper.selectList();
        PageInfo pageInfo=new PageInfo<>(donateLogList);
        List<DonateLogVO> donateLogVOList=donateLogListToDonateLogVOList(donateLogList);
        pageInfo.setList(donateLogVOList);
        return pageInfo;
    }

    @Override
    public void add(Integer id, Integer addMoney) {
        DonateLog donateLog=new DonateLog();
        donateLog.setPrice(addMoney);
        donateLog.setCreateTime(new Date());
        donateLog.setMemberId(id);
        donateLogMapper.insert(donateLog);
    }

    public List<DonateLogVO> donateLogListToDonateLogVOList(List<DonateLog> donateLogList){
        List<DonateLogVO> donateLogVOList=new ArrayList<>();
        for (int i = 0; i < donateLogList.size(); i++) {
            DonateLog donateLog =  donateLogList.get(i);
            donateLogVOList.add(getDonateLogVO(donateLog));
        }
        return donateLogVOList;
    }
    public DonateLogVO getDonateLogVO(DonateLog donateLog){
        DonateLogVO donateLogVO=new DonateLogVO();
        BeanUtils.copyProperties(donateLog,donateLogVO);
        donateLogVO.setPrice((float)donateLog.getPrice()/100);
        MemberVO member=memberService.selectVOById(donateLog.getMemberId());
        donateLogVO.setUserinfo(member);
        donateLogVO.setCreate_time(donateLog.getCreateTime());
        return donateLogVO;
    }

}
